﻿using System;
using Utils.Wpf;

namespace StockAnalyst2.Plugins.Visualization.Historical
{
	internal class SummaryViewerVM : ViewModelBase, IDisposable
	{
		#region Attributes

		private readonly ExecutionResultStatistics _resultStatistics;

		#endregion

		#region Properties

		public int TotalOperations
		{
			get
			{
				return _resultStatistics.TotalOperations;
			}
		}
		
		public int TotalDays
		{
			get
			{
				return _resultStatistics.TotalDays;
			}
		}

		public int WinOperations
		{
			get
			{
				return _resultStatistics.WinOperations;
			}
		}

		public int LossOperations
		{
			get
			{
				return _resultStatistics.LossOperations;
			}
		}

		public decimal TotalResult
		{
			get
			{
				return Math.Round(_resultStatistics.TotalResult, 2);
			}
		}

		public decimal MaxWinOperation
		{
			get
			{
				return Math.Round(_resultStatistics.MaxWinOperation, 2);
			}
		}

		public decimal MaxLossOperation
		{
			get
			{
				return Math.Round(_resultStatistics.MaxLossOperation, 2);
			}
		}

		public decimal AverageOperation
		{
			get
			{
				return Math.Round(_resultStatistics.AverageOperation, 2);
			}
		}

		public decimal TotalWin
		{
			get
			{
				return Math.Round(_resultStatistics.TotalWin, 2);
			}
		}

		public decimal AverageWin
		{
			get
			{
				return Math.Round(_resultStatistics.AverageWin, 2);
			}
		}

		public decimal TotalLoss
		{
			get
			{
				return Math.Round(_resultStatistics.TotalLoss, 2);
			}
		}

		public decimal AverageLoss
		{
			get
			{
				return Math.Round(_resultStatistics.AverageLoss, 2);
			}
		}

		public decimal MaxWin
		{
			get
			{
				return Math.Round(_resultStatistics.MaxWin, 2);
			}
		}

		public decimal MaxLoss
		{
			get
			{
				return Math.Round(_resultStatistics.MaxLoss, 2);
			}
		}

		public decimal MaxAmountRequired
		{
			get
			{
				return Math.Round(_resultStatistics.MaxAmountRequired, 2);
			}
		}

		public decimal Profit
		{
			get
			{
				return Math.Round(_resultStatistics.Profit, 2);
			}
		}

		public decimal DailyProfit
		{
			get
			{
				return Math.Round(_resultStatistics.DailyProfit, 5);
			}
		}

		public decimal WorstResult
		{
			get
			{
				return Math.Round(_resultStatistics.WorstResult, 2);
			}
		}

		public decimal BestResult
		{
			get
			{
				return Math.Round(_resultStatistics.BestResult, 2);
			}
		}

		public decimal WorstResultPercentage
		{
			get 
			{ 
				return _resultStatistics.WorstResultPercentage;
			}
		}

		public decimal BestResultPercentage
		{
			get
			{
				return _resultStatistics.BestResultPercentage;
			}
		}

		public decimal LossOperationsPercentage
		{
			get
			{
				return Math.Round(_resultStatistics.LossOperationsPercentage, 2);
			}
		}

		public decimal WinOperationsPercentage
		{
			get
			{
				return Math.Round(_resultStatistics.WinOperationsPercentage, 2);
			}
		}

		public string WorstResultPeriod
		{
			get
			{
				return _resultStatistics.WorstResultPeriod != null ? string.Format("{0} - {1}", _resultStatistics.WorstResultPeriod.From.ToShortDateString(), _resultStatistics.WorstResultPeriod.To.ToShortDateString()) : String.Empty;
			}
		}

		public string BestResultPeriod
		{
			get
			{
				return _resultStatistics.BestResultPeriod != null ? string.Format("{0} - {1}", _resultStatistics.BestResultPeriod.From.ToShortDateString(), _resultStatistics.BestResultPeriod.To.ToShortDateString()) : String.Empty;
			}
		}

		#endregion

		#region Public Methods

		public SummaryViewerVM(ExecutionResultStatistics resultStatistics)
		{
			_resultStatistics = resultStatistics;
			_resultStatistics.PropertyChanged += this.ResultStatistics_PropertyChanged; // Disposed
		}

		#endregion

		#region Event Handlers

		private void ResultStatistics_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e.PropertyName);
		}

		#endregion

		#region IDisposable

		protected bool Disposed { get; private set; }

		private void Dispose(bool disposing)
		{
			if (!this.Disposed)
			{
				this.InternalDispose(disposing);
			}

			this.Disposed = true;
		}

		protected virtual void InternalDispose(bool disposing)
		{
			if (disposing)
			{
				_resultStatistics.PropertyChanged -= this.ResultStatistics_PropertyChanged;
			}

			// Dispose here only unmanaged objects 
			// Don’t use managed objects here because maybe 
			// they have been finalized already
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		~SummaryViewerVM()
		{
			this.Dispose(false);
		}

		#endregion
	}
}
